package com.wang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.domain.User;
import com.wang.mapper.UserMapper;
import com.wang.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author WangZai
 * @since 2023-03-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public boolean saveUser(User user) {
        return this.save(user);
    }

    @Override
    public boolean deleteUserById(Integer userId) {
        return this.removeById(userId);
    }

    @Override
    public User getUserByUserName(String userName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        return this.getOne(queryWrapper);
    }

    @Override
    public User getUserByPhone(String Phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", Phone);
        return this.getOne(queryWrapper);
    }


    @Override
    public User getUserByEmail(String Email) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", Email);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<User> getUserByCondition(Integer userId, String userName, String realName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (userId == null && userName == null && realName == null) {
            return this.list();
        } else {
            if (userId != null) {
                queryWrapper.like("user_id", userId);
            }
            if (!userName.equals("")) {
                queryWrapper.like("user_name", userName);
            }
            if (!realName.equals("")) {
                queryWrapper.like("real_name", realName);
            }
            return this.list(queryWrapper);
        }
    }

    @Override
    public boolean updateUserById(User user) {
        return this.updateById(user);
    }
}
